package com.czwj.service.impl;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.czwj.dao.AttachmentMapper;
import com.czwj.dao.AttachmentRelyMapper;
import com.czwj.dao.ForumPostMapper;
import com.czwj.dao.ForumReplyMapper;
import com.czwj.entities.Attachment;
import com.czwj.entities.AttachmentRely;
import com.czwj.entities.ForumPost;
import com.czwj.entities.ForumReply;
import com.czwj.service.ForumReplyService;

/**
* @ClassName: ForumReplyServiceImpl
* @Description: TODO
* @author H
* @date 2020年6月1日
*
*/
@Service
public class ForumReplyServiceImpl implements ForumReplyService{
	@Resource
	ForumReplyMapper mapper;
	
	@Resource
	ForumPostMapper postmapper;
	
	@Resource
	AttachmentMapper amapper;
	
	@Resource
	AttachmentRelyMapper relymapper;
	
	@Value("${resources.phypath}")
	private String PhyPath;
	
	@Value("${resources.respath}")
	private String ResPath;
	
	private final String Attachment_ForumPost_Path = "回复/";
	
	/** 
	* 
	* @param reply
	* @see com.czwj.service.ForumReplyService#addForumReply(com.czwj.entities.ForumReply)
	*/
	@Transactional
	@Override
	public Integer add(ForumReply reply){
		// TODO Auto-generated method stub
		int postId = reply.getPostid();
		ForumPost post = postmapper.selectByPrimaryKey(postId);
		post.setReplycount(post.getReplycount() + 1);
		postmapper.updateByPrimaryKeySelective(post);
		return this.mapper.insertSelective(reply);
	}
	
	/** 
	* 
	* @param postId
	* @return
	* @see com.czwj.service.ForumReplyService#listForumReplyByPostId(int)
	*/
	@Override
	public List<ForumReply> search(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.search(map);
	}
	
	/** 
	* 
	* @param replyId
	* @see com.czwj.service.ForumReplyService#deleteForumReply(int)
	*/
	@Transactional
	@Override
	public Integer delete(int replyId){
		// TODO Auto-generated method stub
		ForumReply reply = this.mapper.selectByPrimaryKey(replyId);
		ForumPost post = postmapper.selectByPrimaryKey(reply.getPostid());
		post.setReplycount(post.getReplycount() - 1);
		postmapper.updateByPrimaryKeySelective(post);
		return this.mapper.deleteByPrimaryKey(replyId);
	}
	
	@Transactional
	@Override
	public int insertorupdate(Integer id,Integer postid,String content,Integer parentid,int createrid,String createip,
			MultipartFile[] images){
		// TODO Auto-generated method stub
		int count = 0;
		try{
			ForumReply forumReply = new ForumReply(id,postid,parentid,content,createrid,new Date(),createip);
			count += this.mapper.insertSelective(forumReply);

			ForumPost post = postmapper.selectByPrimaryKey(postid);
			post.setReplycount(post.getReplycount() + 1);
			post.setUpdatetime(new Date());
			postmapper.updateByPrimaryKeySelective(post);
			
			String path = Attachment_ForumPost_Path + forumReply.getCreaterid() + "/";
			String sourcepath = PhyPath + path;// 存储路径
			String urlpath = ResPath + path;// 资源路径
			File dest = new File(sourcepath);
			if(!dest.exists()){
				dest.mkdirs();
			}
			for(MultipartFile image:images){
				String filename = image.getOriginalFilename();
				if(!filename.contains(".")) {
					filename+=".jpg";
				}
				String _sourcepath = sourcepath + filename;
				String _urlpath = urlpath + filename;
				File file = new File(_sourcepath);
				while(file.exists()){
					String[] arr = _sourcepath.split("\\.");
					_sourcepath = "";
					for(int i = 0;i < arr.length;i++){
						if(i < arr.length - 1){
							_sourcepath += arr[i];;
						}else{
							_sourcepath += "(1)." + arr[i];
						}
					}
					file = new File(_sourcepath);
				}
				image.transferTo(new File(_sourcepath));
				Attachment attachment = new Attachment(filename,_urlpath,_sourcepath,"image",createrid,new Date(),
						createip);
				count += this.amapper.insertSelective(attachment);
				AttachmentRely aRely = new AttachmentRely("forumReply",forumReply.getId(),attachment.getId());
				count += this.relymapper.insertSelective(aRely);
			}
		}
		catch(Exception e){
			// TODO Auto-generated catch block
			return 0;
		}
		return count;
	}
	
	/** 
	* 
	* @param map
	* @return
	* @see com.czwj.service.ForumReplyService#count(java.util.Map)
	*/
	@Override
	public int count(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.count(map);
	}
}
